package com.example.factory.api.dto.assembler;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.MD5;
import com.example.factory.api.dto.DtoUser;
import com.example.factory.api.dto.form.DtoUserForm;
import com.example.factory.api.utils.MD5Utils;
import com.example.factory.core.domain.Department;
import com.example.factory.core.domain.RoleEntity;
import com.example.factory.core.domain.UserEntity;
import com.example.factory.core.repository.DepartmentRepository;
import com.example.factory.core.repository.RoleEntityRepository;
import com.example.factory.core.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lei Tao
 */
@Component
public class UserAssembler {
    private final UserRepository userRepository;
    private final DepartmentRepository departmentRepository;
    private final RoleEntityRepository roleEntityRepository;

    public UserAssembler(UserRepository userRepository, DepartmentRepository departmentRepository, RoleEntityRepository roleEntityRepository) {
        this.userRepository = userRepository;
        this.departmentRepository = departmentRepository;
        this.roleEntityRepository = roleEntityRepository;
    }

    public List<DtoUser> toDtoUsers(Page<UserEntity> users) {
        List<UserEntity> userEntities = users.getContent();
        return userEntities.stream().map(this::toDtoUser).collect(Collectors.toList());
    }

    public DtoUser toDtoUser(UserEntity userEntity) {
        DtoUser dtoUser = new DtoUser();
        BeanUtils.copyProperties(userEntity, dtoUser);
        Department department = userEntity.getDepartment();
        if (department!=null){
            dtoUser.setDepartmentName(department.getName());
            dtoUser.setDepartmentGuid(department.getGuid());
        }
        RoleEntity role = userEntity.getRole();
        if (role!=null){
            dtoUser.setRole(role.getName());
            dtoUser.setRoleGuid(role.getGuid());
        }
        String password = userEntity.getPassword();
        MD5 md5 = MD5.create();
        String s = md5.digestHex16(password);
        return dtoUser;
    }

    public UserEntity toUser(DtoUserForm dtoUserForm) {
        UserEntity userEntity;
        if (!StringUtils.isEmpty(dtoUserForm.getGuid())) {
            userEntity = userRepository.findByGuidAndArchivedFalse(dtoUserForm.getGuid());
        } else {
            userEntity = new UserEntity();
        }
        BeanUtils.copyProperties(dtoUserForm, userEntity);
        Department department = departmentRepository.findByGuidAndArchivedFalse(dtoUserForm.getDepartmentGuid());
        RoleEntity role = roleEntityRepository.findByGuidAndArchivedFalse(dtoUserForm.getRoleGuid());
        userEntity.setDepartment(department);
        userEntity.setRole(role);
        String password = dtoUserForm.getPassword();
        String userPassword = MD5Utils.createUserPassword(password);
        userEntity.setPassword(userPassword);
        userEntity.setAccount(dtoUserForm.getAccount());
        return userEntity;
    }
}
